package com.jade.explorer.ui.imp;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import com.jade.explorer.ui.SWTResourceManager;
import com.jade.explorer.views.JadeSharedImages;
import org.eclipse.swt.widgets.Button;

public class ProjectImportDialog extends Dialog implements IImportDialog{
	
	private IImportTarget _target;
	private boolean _result;
	private HashMap<String, ArrayList<File>> _data;
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public ProjectImportDialog(Shell parentShell, String path) {
		super(parentShell);
		
		_target = new ImportProjectHard();
		_target.setLocalPath(path);
	}

	
	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(null);

		//---------------------------
		Composite titleFrame = new Composite(container, SWT.NONE);
		titleFrame.setBounds(11, 13, 64, 64);
		titleFrame.setBounds(0, 0, 444, 44);
		titleFrame.setBackgroundImage(JadeSharedImages.getMenuTitleImg());
		titleFrame.setFont(SWTResourceManager.getFont("Rockwell", 14, SWT.BOLD));
		titleFrame.addPaintListener(new PaintListener() { 
			public void paintControl(PaintEvent e) { 
				e.gc.drawString("Import " + _target.getTitle(), 10, 10, true); 
				} 
		}); 
		//----------------------------
		
		Composite layer = _target.createLayer(container);
		layer.setVisible(true);
		
		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		Button finishButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.FINISH_LABEL, false);
		finishButton.setEnabled(false);
		_target.buttonControl(finishButton);
		createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
		
		
		finishButton.addSelectionListener(new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent e){
				
				
				if (_target.ready()){
					_target.run();
					_result = _target.getResult();
					_data = _target.getImportedData();
					close();
				}
			
				
			}
		});
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 450);
	}
	
	@Override
	public boolean getResult(){
		return _result;
	}
	
	@Override
	public HashMap<String, ArrayList<File>> getImportedData(){
		return _data;
	}


	@Override
	public void openDialog() {
		open();
	}


	@Override
	public int getCode() {
		return getReturnCode();
	}
	
	
}
